package Controller;

import pojo.Card;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class Play {
    private List<Card> box = new ArrayList<>(54);
    private List<List<Card>> players = new ArrayList<>(3);

    public void start() {
        // 1. 准备工作
        prepare();
        // 2. 给玩家随机发牌
        publish();
        // 3. 展示牌
        show();
    }

    private void prepare() {
        for (int i = 1; i < 14; i++) {
            int size = i;
            String str_num = String.valueOf(i);

            switch (i) {
                case 1:
                    size = 15;
                    str_num = "A";
                    break;
                case 2:
                    size = 16;
                    break;
                case 11:
                    str_num = "J";
                    break;
                case 12:
                    str_num = "Q";
                    break;
                case 13:
                    str_num = "K";
                    break;
            }
            Card c1 = getcard("♠", str_num, size);
            Card c2 = getcard("♣", str_num, size);
            Card c3 = getcard("♦", str_num, size);
            Card c4 = getcard("♥", str_num, size);
            box.add(c1);
            box.add(c2);
            box.add(c3);
            box.add(c4);
        }
        // 添加大小王
        Card bigJoker = getcard("joker", "BJ", 17);
        Card smallJoker = getcard("JOKER", "SJ", 18);
        box.add(bigJoker);
        box.add(smallJoker);
    }

    private void publish() {
        // 创建三个玩家的牌堆
        for (int i = 0; i < 3; i++) {
            players.add(new ArrayList<>());
        }

        // 将牌堆 box 中的牌随机分配给三个玩家
        Collections.shuffle(box);
        for (int i = 0; i < 54; i++) {
            players.get(i % 3).add(box.get(i));
        }
    }

    private void show() {
        for (int i = 0; i < 3; i++) {
            System.out.println("玩家 " + (i + 1) + " 的牌是:");
            for (Card card : players.get(i)) {
                System.out.println(card.getPattern()+card.getStr_num());
            }
        }
    }

    private Card getcard(String pattern, String str_num, Integer t_num) {
        Card card = new Card();
        card.setPattern(pattern);
        card.setStr_num(str_num);
        card.setT_num(t_num);
        return card;
    }
}
